package es.wtestgen.action.profesor;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

import es.wtestgen.action.WTestGenAction;
import es.wtestgen.bean.profesor.AlumnoForm;
import es.wtestgen.domain.Alumno;
import es.wtestgen.domain.Asignatura;
import es.wtestgen.domain.Profesor;
import es.wtestgen.services.AlumnoDAO;
import es.wtestgen.services.AsignaturaDAO;
import es.wtestgen.util.DniDuplicadoException;
import es.wtestgen.util.NombreUsuaException;

public class MantenimientoAlumnosAction extends WTestGenAction {
	
	private static final Log log = LogFactory.getLog(MantenimientoAlumnosAction.class);

	@Override
	public ActionForward _execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws NombreUsuaException, DniDuplicadoException  {

		log.info("_____Mantenimiento de alumnos:::::");	
		String accion = request.getParameter("accion");
		ActionForward forward = null;
		
		if(accion == null || accion.equals("")){
			forward = listar(mapping, form, request, response);
		}else if(accion.equals("seleccionarAsignatura")){
			forward = seleccionarAsignatura(mapping, form, request, response);			
		}else if(accion.equals("nuevo")){
			forward = nuevo(mapping, form, request, response);
		}else if(accion.equals("eliminar")){
			forward = eliminar(mapping, form, request, response);
		}else if(accion.equals("editar")){
			forward = editar(mapping, form, request, response);
		}else if(accion.equals("guardar")){
			forward = guardar(mapping, form, request, response);
		}else if(accion.equals("buscar")){
			forward = buscar(mapping,form,request,response);
		}else if(accion.equals("buscarAlumno")){
			forward = buscarAlumno(mapping,form,request,response);
		}else if(accion.equals("anadirAlumno")){
			forward = anadirAlumno(mapping,form,request,response);			
		}else if(accion.equals("eliminarDeAsignatura")){
			forward = eliminarAlumnoDeAsignatura(mapping,form,request,response);
		}else if(accion.equals("verTodos")){
			forward = verTodosAlumnos(mapping,form,request,response);			
		}

		return forward;
	}
	
	
	private ActionForward verTodosAlumnos(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		AlumnoDAO alumnoDao = new AlumnoDAO();
		
		AlumnoForm alumnoForm = (AlumnoForm)form;
		
		List<Alumno> alumnos = alumnoDao.findAll();

		if(alumnos != null && alumnos.size()>0){
			alumnoForm.setAlumnos(alumnos);
		}else{
			alumnoForm.setAlumnos(new ArrayList<Alumno>());
		}
		request.setAttribute("alumnos", alumnoForm.getAlumnos());
		return mapping.findForward("verTodosAlumnos");
	}


	private ActionForward eliminarAlumnoDeAsignatura(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		AlumnoDAO alumnoDao = new AlumnoDAO();
		AlumnoForm alumnoForm = (AlumnoForm) form;
		
		String codAlum = request.getParameter("codAlum");
		String codAsig = (String)request.getParameter("codAsig");
		
		/*
		 * Con esta clase que hereda de ActionForward le podemos añadir parametros a las
		 * redirecciones forward
		 */
		ActionRedirect actionRedirect = null;
		
		if(alumnoDao.eliminarAlumnoDeAsignatura(Integer.parseInt(codAlum),Integer.parseInt(codAsig))){
			ActionForward findForward = mapping.findForward("eliminadoAlumno");
			actionRedirect = new ActionRedirect(findForward);
			actionRedirect.addParameter("accion", "");
			actionRedirect.addParameter("eliminadoOk", "si");
			actionRedirect.addParameter("codAsig", codAsig);
			return actionRedirect;			
		}else{
			return mapping.findForward("error");
		}
	}


	private ActionForward anadirAlumno(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AlumnoDAO alumnoDao = new AlumnoDAO();
		
		AlumnoForm alumnoForm = (AlumnoForm) form;
		
		String codAlum = request.getParameter("codAlum");
		String codAsig = (String)request.getParameter("codAsig");
		
		Alumno alum = (Alumno)alumnoDao.findById(codAlum).get(0);
		/*
		 * Con esta clase que hereda de ActionForward le podemos añadir parametros a las
		 * redirecciones forward
		 */
		ActionRedirect actionRedirect = null;
		
		if(alumnoDao.guardarAlumnoExistenteEnAsignatura(alum, codAsig)){
			ActionForward findForward = mapping.findForward("guardadoAlumnoEnAsignatura");
			actionRedirect = new ActionRedirect(findForward);
			actionRedirect.addParameter("accion", "");
			actionRedirect.addParameter("guardadoOk", "si");
			actionRedirect.addParameter("codAsig", codAsig);
			return actionRedirect;			
		}else{
			return mapping.findForward("error");
		}
	}


	private ActionForward buscarAlumno(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		AlumnoDAO alumnoDao = new AlumnoDAO();
		AlumnoForm alumnoForm = (AlumnoForm)form;
		
		List<Alumno> alumnos = alumnoDao.findByParameters(alumnoForm);
		
		if(alumnos != null && alumnos.size()>0){
			alumnoForm.setAlumnos(alumnos);
		}else{
			alumnoForm.setAlumnos(new ArrayList<Alumno>());
		}
		request.setAttribute("alumnos", alumnoForm.getAlumnos());
		
		
		return mapping.findForward("buscarAlumno");
	}


	private ActionForward buscar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		return mapping.findForward("buscarAlumno");
	}


	public ActionForward nuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			 {
		
		AlumnoForm alumnoForm = (AlumnoForm) form;
		Alumno alumno = new Alumno();
		
		request.setAttribute("alumno", alumno);
		return mapping.findForward("nuevoAlumno");
	}

	public ActionForward eliminar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		AlumnoDAO alumnoDao = new AlumnoDAO();
		
		AlumnoForm alumnoForm = (AlumnoForm) form;		
		
		String codAlum = request.getParameter("codAlum");
		String codAsig = (String)request.getParameter("codAsig");
		
		/*
		 * Con esta clase que hereda de ActionForward le podemos añadir parametros a las
		 * redirecciones forward
		 */
		ActionRedirect actionRedirect = null;
		
		if(alumnoDao.eliminar(Integer.parseInt(codAlum)/*,Integer.parseInt(codAsig)*/)){
			ActionForward findForward = mapping.findForward("eliminadoAlumno");
			actionRedirect = new ActionRedirect(findForward);
			actionRedirect.addParameter("accion", "");
			actionRedirect.addParameter("eliminadoOk", "si");
			if(codAsig != null && "".equals(codAsig)){
				actionRedirect.addParameter("codAsig", codAsig);				
			}
			return actionRedirect;			
		}else{
			//return mapping.findForward("error");
			return mapping.findForward("errorEliminarAlumno");
			
		}
	}
	
	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{	
		AlumnoDAO alumnoDao = new AlumnoDAO();
		
		AlumnoForm alumnoForm = (AlumnoForm) form;
		
		String codAlum = request.getParameter("codAlum");
		String codAsig = request.getParameter("codAsig");
		
		List<Alumno> alumnos = alumnoDao.findById(codAlum);		
		Alumno alumno = alumnos.get(0);
		
		alumnoForm.setApellidos(alumno.getApellidos());
		alumnoForm.setNombre(alumno.getNombre());
		alumnoForm.setNombreUsua(alumno.getNombreUsua());
		alumnoForm.setCodAlum(String.valueOf(alumno.getCodAlum()));
		alumnoForm.setDni(alumno.getDni());
		alumnoForm.setEmail(alumno.getEmail());
		alumnoForm.setTelefono(alumno.getTelefono());
		alumnoForm.setPwdUsua(alumno.getPwdUsua());
		alumnoForm.setConfirmPwdUsua(alumno.getPwdUsua());
		
		request.setAttribute("alumnoForm", alumnoForm);
		return mapping.findForward("nuevoAlumno");
	}
	
	public ActionForward guardar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws NombreUsuaException,DniDuplicadoException
	{
		AlumnoDAO alumnoDao = new AlumnoDAO();
		
		AlumnoForm alumnoForm = (AlumnoForm) form;
		
		String codAsig = (String)request.getParameter("codAsig");
		
		Alumno alumno = new Alumno();
		
		alumno.setNombre(alumnoForm.getNombre());
		alumno.setApellidos(alumnoForm.getApellidos());
		alumno.setDni(alumnoForm.getDni());
		alumno.setEmail(alumnoForm.getEmail());
		alumno.setTelefono(alumnoForm.getTelefono());
		alumno.setNombreUsua(alumnoForm.getNombreUsua());
		alumno.setPwdUsua(alumnoForm.getPwdUsua());		
		if(alumnoForm.getCodAlum() != null && !alumnoForm.getCodAlum().equals("") && !alumnoForm.getCodAlum().equals("0")){
			int id = Integer.parseInt(alumnoForm.getCodAlum());
			alumno.setCodAlum(id);
		}else{
			alumno.setCodAlum(-1);
		}
		
		ActionRedirect actionRedirect = null;
		if(alumnoDao.guardarAlumnoEnAsignatura(alumno,codAsig)){			
			ActionForward findForward = mapping.findForward("guardadoAlumno");
			actionRedirect = new ActionRedirect(findForward);
			actionRedirect.addParameter("accion", "");
			actionRedirect.addParameter("guardadoOk", "si");
			actionRedirect.addParameter("codAsig", codAsig);
			return actionRedirect;			
		}else{
			return mapping.findForward("error");
		}
		
	}

	public ActionForward listar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		ActionForward forward = null;
		
		AlumnoDAO alumnoDao = new AlumnoDAO();
		
		AsignaturaDAO asignaturaDao = new AsignaturaDAO();
		
		AlumnoForm alumnoForm = (AlumnoForm)form;
		
		String codAsig = request.getParameter("codAsig");

		if(codAsig == null || codAsig.equals("")){			
			List <Alumno>listaAlumnos = alumnoDao.findAll();			 
			if(listaAlumnos != null && listaAlumnos.size()>0){
				alumnoForm.setAlumnos(listaAlumnos);
			}else{
				alumnoForm.setAlumnos(new ArrayList<Alumno>());
			}
			forward = mapping.findForward("verTodosAlumnos");			
		}else{
			List<Asignatura> asignaturas = asignaturaDao.findById(codAsig);
			Asignatura asignatura = asignaturas.get(0);
			
			Set<Alumno> alumnos = alumnoDao.findAlumnosAsignatura(asignatura);		
			List <Alumno>listaAlumnos = new ArrayList<Alumno>(alumnos);
			if(alumnos != null && alumnos.size()>0){
				alumnoForm.setAlumnos(listaAlumnos);
			}else{
				alumnoForm.setAlumnos(new ArrayList<Alumno>());
			}
			request.getSession().setAttribute("asignatura", asignatura);		
			forward = mapping.findForward("listarAlumnos");
		}		
		
		request.setAttribute("alumnos", alumnoForm.getAlumnos());
		return forward;
	}
	
	
	public ActionForward seleccionarAsignatura(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		AlumnoForm alumnoForm = (AlumnoForm)form;
		
		AsignaturaDAO asignaturaDAO = new AsignaturaDAO();
		AlumnoDAO alumnoDAO = new AlumnoDAO();
		
		List<Alumno> alumnos = alumnoDAO.findAll();
		if(alumnos != null){
			alumnoForm.setAlumnos(alumnos);
		}else{
			alumnoForm.setAlumnos(new ArrayList<Alumno>());
		}
		
		Profesor profesor = (Profesor)request.getSession().getAttribute("usuario");
		
		List<Asignatura> asignaturas = asignaturaDAO.findAsignaturasProfesor(profesor);
		
		if(asignaturas != null && asignaturas.size()>0){
			alumnoForm.setAsignaturas(asignaturas);
		}else{
			alumnoForm.setAsignaturas(new ArrayList<Asignatura>());
		}
		request.setAttribute("asignaturas", alumnoForm.getAsignaturas());
		return mapping.findForward("seleccionarAsignaturas");
	}
	

}
